<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util;
vendor('taobao.TopSdk');//引入淘宝SDK
class OrdersController extends Controller {
	//构造函数
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			//$this->redirect("Login/login"); 
			/*echo "<script>alert('对不起,您还没有登录!请先登录!');top.location.href='index.php/Home/Login/login.html';</script>";
			*/
			 /*echo "<script>alert('对不起,您还没有登录!请先登录!');top.location.href='index.php/Home/Login/login.html';</script>";*/
			 //$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
		}
	}

	//新建订单首页
	public function order_list(){
		$pageNo		=	empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize	=	empty($_REQUEST['pageSize'])?50:$_REQUEST['pageSize'];
		$name 		= 	$_SESSION['user'];
		$account 	= 	M("account")->field("orders")->where(array('username'=>$name))->find();
		$orders 	= 	$account['orders'];
		if($orders=='2'){
			$model = M("order_info");
			$where['status']='1';
			$where['salesman'] = $name;
			$order = D("orders");
			$res = $order->order_list($pageNo,$pageSize,$where);
		}else{
			$model = M("order_info");
			$where['status']='1';
			$order = D("orders");
			$res = $order->order_list($pageNo,$pageSize,$where);
		}
		$count = $res['count'];
		$order_list = $res['order_list'];
		$total_value = '';
		foreach($order_list as $key=>$val){
			$order_number = $val['order_number'];
			$price_list = $model->where(array("order_number"=>$order_number))->sum('total_price');
			$order_list[$key]['total_price']=$price_list;
			
			
			$str =$order_list[$key]['total_price']*$val['discount_rate'];
			$arr = explode('.',$str);
			$num=substr($arr[1],0,2);
			$discount_rate = $arr[0].'.'.$num;
			
			$order_list[$key]['discounted_value'] = $discount_rate;
		}
		$this->assign("order_list",$order_list);
		$this->assign("count",$count);
		$this->display();
	}
	
	public function look_down(){
		error_reporting(E_ALL);
        date_default_timezone_set('PRC');
        $order_number = I('get.order_number','','htmlspecialchars');
		$order_info = M('order')->where(array('order_number'=>$order_number,'sales_return'=>''))->find();
		$join = " left join product on product.id = order_info.p_id";
		$data = M('order_info')->join($join)->where(array('order_number'=>$order_number,'sales_return'=>''))->select();
		$total_price = M('order_info')->where(array('order_number'=>$order_number,'sales_return'=>''))->SUM('total_price');
        // 导出Exl
		
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
        
        // 水平居中（位置很重要，建议在最初始位置）
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        // 垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        
		//合并列单元
		//设置宽度
		$objActSheet->getColumnDimension('A')->setWidth(15);
		$objActSheet->getColumnDimension('B')->setWidth(21);
		$objActSheet->getColumnDimension('C')->setWidth(20);
		$objActSheet->getColumnDimension('D')->setWidth(20);
		$objActSheet->getColumnDimension('E')->setWidth(20);
		$objActSheet->getColumnDimension('F')->setWidth(20);
		$objActSheet->getColumnDimension('G')->setWidth(20);
		
		$objActSheet->getColumnDimension('A1')->setWidth(15);
		
		$objPHPExcel->getActiveSheet()->getColumnDimension('A1')->setWidth(10);
		$objDrawing[1] = new \PHPExcel_Worksheet_Drawing();
        $objDrawing[1]->setPath('Public/img/logo.png');
        // 设置宽度高度
        $objDrawing[1]->setHeight(100);//照片高度
        $objDrawing[1]->setWidth('120'); //照片宽度
        /*设置图片要插入的单元格*/
		
        $objDrawing[1]->setCoordinates('A1');
		
        // 图片偏移距离
        $objDrawing[1]->setOffsetX(30);
        $objDrawing[1]->setOffsetY(0);
        $objDrawing[1]->setWorksheet($objPHPExcel->getActiveSheet());
		$objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
        $objActSheet->setCellValue('A1', 'Touch Love Jewelry Co., Ltd.');
		$objPHPExcel->getActiveSheet()->mergeCells('A2:G2');
		$objActSheet->setCellValue('A2', 'Add: R801 Tairong business building, No.63 Xizeng Road, GuangZhou, China');			
		$objPHPExcel->getActiveSheet()->mergeCells('A3:G3');
		$objActSheet->setCellValue('A3', 'Tel : +86-20-81537159');
		$objPHPExcel->getActiveSheet()->mergeCells('A4:G4');
		$objActSheet->setCellValue('A4', 'Email : sale@touch-love-jewelry.com ');
		
		$objPHPExcel->getActiveSheet()->mergeCells('A5:G5');
		$objActSheet->setCellValue('A5', 'Website: http://touchlove.en.alibaba.com');
		$objPHPExcel->getActiveSheet()->mergeCells('A6:G6');
		$objActSheet->setCellValue('A6', 'Proforma Invoice');
		
		
		// 垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A7')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('E7')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('E8')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('A9')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->getActiveSheet()->getStyle('A10')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		
		$objPHPExcel->getActiveSheet()->mergeCells('A7:D7');
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('A7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->getActiveSheet()->getStyle('A7')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
        $objActSheet->setCellValue('A7', 'To:'.$order_info['customer_name']);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('E7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->getActiveSheet()->mergeCells('E7:G7');
		$objPHPExcel->getActiveSheet()->getStyle('E7')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
        $objActSheet->setCellValue('E7', 'PI NO.:'.$order_info['order_number']);
		
		$objPHPExcel->getActiveSheet()->mergeCells('A8:D8');
		$objPHPExcel->getActiveSheet()->getStyle('A8')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('A8')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objActSheet->setCellValue('A8', 'Att:'.$order_info['consignee']);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('E8')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->getActiveSheet()->mergeCells('E8:G8');
		$objPHPExcel->getActiveSheet()->getStyle('E8')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
        $objActSheet->setCellValue('E8', 'Email:'.$order_info['email']);
		
		$objPHPExcel->getActiveSheet()->mergeCells('A9:D9');
		$objPHPExcel->getActiveSheet()->getStyle('A9')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('A9')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objActSheet->setCellValue('A9', 'Tel:'.$order_info['phone']);
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('E9')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objPHPExcel->getActiveSheet()->getStyle('E9')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objPHPExcel->getActiveSheet()->mergeCells('E9:G9');
        $objActSheet->setCellValue('E9', 'Date:'.$order_info['order_date']);
		
		$objPHPExcel->getActiveSheet()->mergeCells('A10:G10');
		$objPHPExcel->getActiveSheet()->getStyle('A10')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('A10')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objActSheet->setCellValue('A10', 'Add:'.$order_info['address']);
		
		
		$objActSheet->setCellValue('A11', 'Item');
		$objPHPExcel->getActiveSheet()->getStyle('A11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objActSheet->setCellValue('B11', 'Photo');
		$objPHPExcel->setActiveSheetIndex(0)->getStyle('B11')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('B11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objActSheet->setCellValue('C11', 'Specification');
		$objPHPExcel->getActiveSheet()->getStyle('C11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objActSheet->setCellValue('D11', "Unit Price"."\r\n"."(FOB)");
		$objPHPExcel->getActiveSheet()->getStyle('D11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objActSheet->setCellValue('E11', 'Quantity');
		$objPHPExcel->getActiveSheet()->getStyle('E11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objActSheet->setCellValue('F11', 'Amount（US$）');
		$objPHPExcel->getActiveSheet()->getStyle('F11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		$objActSheet->setCellValue('G11', "Weight"."\r\n"."(G)");
		$objPHPExcel->getActiveSheet()->getStyle('G11')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		
		//字体样式
		$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'20'

                ),

            )

        );
		
		$objPHPExcel->getActiveSheet()->getStyle('A6')->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'20'

                ),

            )

        );
        // 处理表数据
        foreach($data as $k=>$v){
            $k +=12;
			
			
			
			$dir = "public/images/".$v['sku']; // 文件夹的名称
			$file = array();
			if (is_dir($dir)){
			  if ($dh = opendir($dir)){
				while (($file[] = readdir($dh)) !== false){
				}
				closedir($dh);
			  }
			}
			
			
			if( in_array("A.jpg",$file) || in_array("A.jpeg",$file) || in_array("A.png",$file) || in_array("A.gif",$file)){
				$objActSheet->getRowDimension('A'.$k)->setRowHeight(80);
				$objActSheet->setCellValue('A'.$k, $v['sku']);    
				$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
				$path = $v['sku'].'/A.jpg';
				$objDrawing[$k]->setPath('Public/images/'.$path);
				// 设置宽度高度
				$objDrawing[$k]->setHeight('110pt');//照片高度
				$objDrawing[$k]->setWidth('140pt'); //照片宽度
				/*设置图片要插入的单元格*/
				$objDrawing[$k]->setCoordinates('B'.$k);
				// 图片偏移距离
				$objDrawing[$k]->setOffsetX(5);
				$objDrawing[$k]->setOffsetY(5);
				$objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet()); 
				}else{
					$objActSheet->setCellValue('A'.$k, $v['sku']); 
					$objActSheet->setCellValue('B'.$k, '图片丢失'); 
					
					$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.$k)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
					
					$objPHPExcel->getActiveSheet()->getStyle('B'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
					
			}
			
			
			//$objActSheet->getRowDimension('A'.$k)->setRowHeight(80);
//            $objActSheet->setCellValue('A'.$k, $v['sku']);    
//            $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
//			$path = $v['sku'].'/A.jpg';
//            $objDrawing[$k]->setPath('Public/images/'.$path);
//            // 设置宽度高度
//            $objDrawing[$k]->setHeight('110pt');//照片高度
//            $objDrawing[$k]->setWidth('140pt'); //照片宽度
//            /*设置图片要插入的单元格*/
//            $objDrawing[$k]->setCoordinates('B'.$k);
//            // 图片偏移距离
//            $objDrawing[$k]->setOffsetX(1);
//            $objDrawing[$k]->setOffsetY(1);
//            $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            $Specification = "Jewelry Type:".$v['product_type']."\r\n Material:".$v['product_material'];
            // 表格内容
			
        	$objPHPExcel->getActiveSheet()->getStyle('C'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objActSheet->setCellValue('C'.$k, $Specification); 
			
			$objActSheet->setCellValue('D'.$k, $v['purchase_price']); 
			
			$objActSheet->setCellValue('E'.$k, $v['quantity']); 
			$objActSheet->setCellValue('F'.$k, $v['total_price']); 
			$objActSheet->setCellValue('G'.$k, $v['weight']*$v['quantity']); 	
			$total_weight = $v['weight']*$v['quantity'];
			$total += $total_weight;
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight('112pt');
            
        }
		
		
			
				
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+1).':E'.($k+1));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+1))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+1))->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
			$objActSheet->setCellValue('A'.($k+1), 'Product Cost'); 
			$objActSheet->setCellValue('F'.($k+1), $total_price); 
			
			$objPHPExcel->getActiveSheet()->mergeCells('G'.($k+1).':G'.($k+5));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+2))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+2))->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
			$objActSheet->setCellValue('G'.($k+1), $total); 
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+2).':E'.($k+2));
			
		
			$str = $v['discount_rate'];
			$discount_rate = ($str*100).'%';
			$str = $total_price*$v['discount_rate'];
			$arr = explode('.',$str);
			$num1=substr($arr[1],0,2);
			$buyer_total = $arr[0].'.'.$num1;
			$objActSheet->setCellValue('A'.($k+2), $discount_rate.' discount'); 
			$objActSheet->setCellValue('F'.($k+2), $buyer_total); 
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+3).':E'.($k+3));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+3))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+3))->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
			
			$objActSheet->setCellValue('A'.($k+3), 'DHL Shipping Cost'); 
			$objActSheet->setCellValue('F'.($k+3), $order_info['feright']);
			$objActSheet->setCellValue('G'.($k+3), $total_price); 
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+4))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+4))->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+4).':E'.($k+4));
			$objActSheet->setCellValue('A'.($k+4), 'Alibaba handing fee');  
			$objActSheet->setCellValue('F'.($k+4), ''); 
			
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+5).':E'.($k+5));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+5))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+5))->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
			$objActSheet->setCellValue('A'.($k+5), 'Total Cost');  
			
			$str1 = $order_info['feright']+$buyer_total;
			$arr1 = explode('.',$str1);
			$num2=substr($arr1[1],0,2);
			$total_cost = $arr1[0].'.'.$num2;
			$objActSheet->setCellValue('F'.($k+5), $total_cost); 
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+6).':G'.($k+6));
			$objActSheet->setCellValue('A'.($k+6), '');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+7).':G'.($k+7));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+7))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+7), 'Remarks:');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+8).':G'.($k+8));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+8))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+8), '1.Package: the above price includes neutral packaging.If you need OEM packaging and print LOGO,pls check with us.');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+9).':G'.($k+9));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+9))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+9), '2.Payment term: 100% of  stock order and 30% deposit of  OEM order in advance, 70% balance payment before shippment (T/T, Western Union, Paypal, etc);	');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+10).':G'.($k+10));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+10))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+10), '3.Delivery lead time: 1-3 working days for sample or stock; about 20-25 days for OEM order after the payment received.');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+11).':G'.($k+11));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+11))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+11), '4.Shippping methods: Express (DHL, UPS, TNT, FedEX, EMS), Sea and Air.');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+12).':G'.($k+12));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+12))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+12), '5.Price validity: 30 days from the date of this quotation.');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+13).':G'.($k+13));
			$objActSheet->setCellValue('A'.($k+13), '');  
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+14).':G'.($k+14));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+14))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			
			// 新建订单的 导出excel 的 账号信息
			$objActSheet->setCellValue('A'.($k+14), 'Account details for orders:');  
			
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+14))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+15).':G'.($k+15));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.($k+15))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('A'.($k+15), 'T/T: ($30 handle fee is needed for each payment)');  
			
			
			
			
			
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+16).':A'.($k+21));
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+16))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objActSheet->setCellValue('A'.($k+16), 'Bank Details');
			
			
			$objActSheet->setCellValue('B'.($k+16), 'Company Name:');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+16))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('B'.($k+16))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'11'

                ),

            )

        );
			$objPHPExcel->getActiveSheet()->mergeCells('C'.($k+16).':G'.($k+16));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+16))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+16))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objActSheet->setCellValue('C'.($k+16), 'TOUCH LOVE JEWELRY CO., LIMITED');
			
			
			
			$objActSheet->setCellValue('B'.($k+17), 'Beneficiary Bank:');
			$objPHPExcel->getActiveSheet()->getStyle('B'.($k+17))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+17))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+17))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+17))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objPHPExcel->getActiveSheet()->mergeCells('C'.($k+17).':G'.($k+17));
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+18))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objActSheet->setCellValue('C'.($k+17), 'HSBC Hong Kong');
			
			$objPHPExcel->getActiveSheet()->getStyle('B'.($k+18))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objActSheet->setCellValue('B'.($k+18), 'Branch code');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+18))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+18))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+19))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->mergeCells('C'.($k+18).':G'.($k+18));
			$objActSheet->setCellValue('C'.($k+18), " 004");
			
			
			$objPHPExcel->getActiveSheet()->getStyle('B'.($k+19))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objActSheet->setCellValue('B'.($k+19), 'Bank Account No:');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+19))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+19))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+19))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objPHPExcel->getActiveSheet()->mergeCells('C'.($k+19).':G'.($k+19));
			$objActSheet->setCellValue('C'.($k+19), '817-810799-838');
			
			
			
			$objPHPExcel->getActiveSheet()->getStyle('B'.($k+20))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			
			$objActSheet->setCellValue('B'.($k+20), 'Bank Address:');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+20))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+20))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
		 	
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+20))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->mergeCells('C'.($k+20).':G'.($k+20));
			$objActSheet->setCellValue('C'.($k+20), "1 Queen's Road Central, HongKong");
			
			
			$objPHPExcel->getActiveSheet()->getStyle('B'.($k+21))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objActSheet->setCellValue('B'.($k+21), 'Swift BIC:');
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+21))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+21))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.($k+21))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$objPHPExcel->getActiveSheet()->mergeCells('C'.($k+21).':G'.($k+21));
			$objPHPExcel->getActiveSheet()->getStyle('C'.($k+21))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'12'

                ),

            )

        );
			$objActSheet->setCellValue('C'.($k+21), 'HSBCHKHHHKH');
			
			
			$objPHPExcel->getActiveSheet()->mergeCells('A'.($k+22).':G'.($k+22));
			$objActSheet->setCellValue('A'.($k+22), '');
			
			
			
			
			$objActSheet->setCellValue('A'.($k+23), 'Western Union');
			$objPHPExcel->getActiveSheet()->mergeCells('B'.($k+23).':G'.($k+23));
			$objActSheet->setCellValue('B'.($k+23), 'Please contact us to get Account Information.');

			
			
			$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.($k+23))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			
			$objPHPExcel->getActiveSheet()->getStyle('A'.($k+23))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'11'

                ),

            )

        );
		
		$objPHPExcel->getActiveSheet()->getStyle('B'.($k+23))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'14'

                ),

            )

        );


        //账号信息修改处  @falcon
		$objActSheet->setCellValue('A'.($k+25), 'Escrow');
		$objPHPExcel->getActiveSheet()->mergeCells('B'.($k+25).':G'.($k+25));
		$objActSheet->setCellValue('B'.($k+25), 'You can use Credit Card and pay on our alibaba website.contact us for more detail.    http://touchlove.en.alibaba.com/  ');

        $objPHPExcel->getActiveSheet()->getStyle('A'.($k+25))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'11'

                ),

            )

        );
		
		$objPHPExcel->getActiveSheet()->getStyle('B'.($k+25))->applyFromArray(

            array(

                'font' => array (

                    'bold' => true,
					"color" => array("rgb" => "red"),
					'size'=>'14'

                ),

            )

        );
			
		
		
        
        $fileName = '订单明细表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";

        //$fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        // END    
    }
	
	
	
	
	
	//新增订单页面
	public function add_order(){
		//查询支付方式
		$payment_list = M("payment")->field('id,payment_method')->select();
		
		if($_POST){
			$data['salesman'] 		= $_SESSION['user'];
			$data['customer_name'] 	= I('post.customer_name','','htmlspecialchars');
			$data['order_number']	= I('post.order_number','','htmlspecialchars');
			$data['consignee'] 		= I('post.consignee','','htmlspecialchars');
			$data['email'] 			= I('post.email','','htmlspecialchars');
			$data['phone'] 			= I('post.phone','','htmlspecialchars');
			$data['order_date'] 	= date('Y-m-d H:i:s');
			$data['address'] 		= I('post.address','','htmlspecialchars');
			$discount_rate 			= I('post.discount_rate','','htmlspecialchars');
			$data['discount_rate'] 	= empty($discount_rate)?'1':$discount_rate;
			$data['feright'] 		= I('post.feright','','htmlspecialchars');
			$data['pay_method'] 	= I('post.payment_method','','htmlspecialchars');
			$where['status'] 		= '1';
			
			$orders_information 	= I('post.orders_information','','htmlspecialchars');
			
			$number = $data['order_number'];
			$order = D("orders");
			//查询订单表内是否存在该订单号
			$order_number = $order->order_number($number);
			$order_number1 = $order->order_number1($number);
			if(!empty($data['order_number']) && $order_number==NULL && $order_number1==NULL){
				
				
			$order_list=explode("\n",trim($orders_information,"\n"));
			
			$count = count($order_list);
			foreach($order_list as $k=>$v){
				$res = explode("	",trim($v,"	"));
				$sku =strtoupper($res['0']);
				$product = M("product");
				$where['sku'] = $sku;
				$product_info = $product->where($where)->find();
				$p_id = $product_info['id'];
				$supplier = $product_info['supplier'];
				$buyer = $product_info['buyer'];
				$main_pic = $product_info['size_pic'];
				$cost_price = $product_info['cost_price'];
				if(!empty($product_info)){
					$result = explode("	",trim($v," "));
					$sku = strtoupper($result['0']);
					$quantity = trim($result['1']);
					$order_info = D("orders");
					//新增产品 有相同的就合并，并且根据价格公式计算价格
					$result_info = $order_info->add_info($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price);
					$result_list[$k] = $result_info;
					
				}else{
					$msg[$k] =  $sku."<br/>";
					
					continue;
				}
				
			}
			
			$orders = M("order");
			$data['status'] = '1';
			$result = $orders->add($data);
			 if(empty($msg)){
				 	$this->success("添加成功","order_list",'3');exit();
				}else{
					echo "<div style='font-size:26px;'>以下产品不存在 ,请及时复制出以下产品,检查后编辑对应订单!</div><br/>";
					foreach($msg as $k=>$v){
						echo $v;
					}
					exit();
				}
			
				
			}else{
				$this->error("添加失败，订单号已存在或商品为空！","order_list",'3');exit();
			}
			
			
			
			
			
			
		}
		
		$this->assign("payment_list",$payment_list);
		$this->display();
	}
	
	//草稿箱订单明细
	public function order_details(){
		$where['id']=$_REQUEST['id'];
		$order = M("order");
		$number = $order->field("order_number")->where($where)->find();
		$order_number = $number["order_number"];
		$order_model = D("orders");
		$order_details = $order_model->details_info($order_number);
		$this->assign("order_details",$order_details);
		$this->display();
	}
	
	
	//草稿箱完成转变为待采购订单
	public function order_finish(){
		$where['id']=$_REQUEST['id'];
		$data['status'] = '2';
		$data['create_time'] = date(('Y-m-d H:i:s'),time());
		$account_type = M('account')->field('new_finish')->where(array('username'=>$_SESSION['user']))->find();
		if($account_type['new_finish']!='1'){$this->error('权限不足，无法点击完成！');die;}
		if(I('get.operate','','htmlspecialchars')){
			
			$order_number = I('get.order_number','','htmlspecialchars');
			$order_info = M('order_info')->where(array('order_number'=>$order_number))->select();
			foreach($order_info as $val){
				$result = M('order_info')->where(array('id'=>$val['id']))->save(array('first_quantity'=>$val['quantity'],'alsobuy'=>$val['quantity']));
				
			}
			$res = M('order_info')->where(array('order_number'=>$order_number))->save(array('status'=>'2'));
		}
		
		$order = M("order");
		$result = $order->where($where)->save($data);
		
		$this->success	("提交成功！");
	}
	
	//新增订单修改
	public function order_edit(){
		$payment_list = M("payment")->field('id,payment_method')->select();
		if(I('post.submit','','htmlspecialchars')){
			$data['customer_name'] = I('post.customer_name','','htmlspecialchars');
			$data['order_number'] = I('post.order_number','','htmlspecialchars');
			$data['consignee'] = I('post.consignee','','htmlspecialchars');
			$data['email'] = I('post.email','','htmlspecialchars');
			$data['phone'] = I('post.phone','','htmlspecialchars');
			$data['feright'] = I('post.feright','','htmlspecialchars');
			$discount_rate = I('post.discount_rate','','htmlspecialchars');
			$data['discount_rate'] 	= empty($discount_rate)?'1':$discount_rate;
			$data['payment_method'] = I('post.payment_method','','htmlspecialchars');
			$data['address'] = I('post.address','','htmlspecialchars');
			$orders_information = I('post.orders_information','','htmlspecialchars');
			$order_list=explode("\n",trim($orders_information,"\n"));
			//修改订单信息
			$edit_info = M('order')->where(array('order_number'=>$data['order_number']))->save($data);
			//修改折扣率
			$rate = M('order_info')->where(array('order_number'=>$data['order_number']))->save(array('discount_rate'=>$data['discount_rate']));
			foreach($order_list as $k=>$v){
				$res = explode("	",trim($v,"	"));
				$sku =$res['0'];
				$product = M("product");
				$where['sku'] = $sku;
				$where['status'] = '1';
				$product_info = $product->where($where)->find();
				$p_id = $product_info['id'];
				$supplier = $product_info['supplier'];
				$buyer = $product_info['buyer'];
				$main_pic = $product_info['size_pic'];
				$cost_price = $product_info['cost_price'];
				if(!empty($product_info)){
					$result = explode("	",trim($v," "));
					$sku = $result['0'];
					$quantity = trim($result['1']);
					//查找订单内是否已有重复的SKU
					$order_info = M('order_info')->where(array('sku'=>$sku,'order_number'=>$data['order_number']))->find();
					
					if($order_info){//如果存在，即修改
						$order_info = D("orders");
						$result_info = $order_info->edit_info($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price);
					}else{//如果不存在，即新增
						$order_info = D("orders");
						$result_info = $order_info->edit_add($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price);
					}
					
				}else{
					$title[] = $sku;
					$msg[$k] =  $sku."<br/>";
					
					continue;
				}
				 
			}
			
			if(empty($msg)){
				$this->success("添加成功","order_list",'3');exit();
			}else{
				$order_list=explode("\n",trim($orders_information,"\n"));
				if($order_list['0']=='' && empty($msg)){
					$this->success("修改成功","order_list",'3');exit();
				}else{
					echo "<div style='font-size:26px;'>以下产品不存在 ,请及时复制出以下产品,检查后编辑对应订单!</div><br/>";
					foreach($msg as $k=>$v){
						echo $v;
					}
					exit();
				}
				
			}
		}
		
		
		$order = M("order");
		$where['id']=$_REQUEST['id'];
		$k=$_REQUEST['k'];
		$order_details = $order->field("order_number")->where($where)->find();
		$order_number = $order_details['order_number'];
		$model = M("order_info");
		$order_info = $model->where(array('order_number'=>$order_number))->group('sku')->select();
		foreach($order_info as $key=>$val){
			$SUM = $model->field('quantity')->where(array('order_number'=>$order_number,'sku'=>$val['sku']))->SUM('quantity');
			$order_info[$key]['quantity'] = $SUM;
			$type = M('product')->field('product_type,product_material')->where(array('order_number'=>$order_number,'sku'=>$val['sku']))->find();
			$order_info[$key]['product_type'] = $type['product_type'];
			$order_info[$key]['product_material'] = $type['product_material'];
		}
		
		$info_list = M('order')->where(array('order_number'=>$order_number))->find();
		$count = count($order_info);
		
		if(I("dele",'','htmlspecialchars')){
			$id = I("dele",'','htmlspecialchars');
			$result = M("order_info")->where(array('id'=>$id))->delete();
			$this->success("删除成功");exit();
		}	
		
		if(I("edit","","htmlspecialchars")){
			$id = I("id","","htmlspecialchars");
			$sku = I("sku","","htmlspecialchars");
			$quantity = I("quantity","","htmlspecialchars");
			$purchase_price	=	I('purchase_price','','htmlspecialchars');
			$order = array();
			foreach($id as $k=>$v){
				$id = $v;
				//查询出子订单的信息purchase_price指导价
				$sku_list = M('order_info')->field('id,sku,cost_price,purchase_price,quantity')->where(array('id'=>$id))->find();
				$info_old = $sku_list;
				//对子订单的数据重新赋值
				$sku_list['quantity']=$quantity[$k];
				$arr_purchase=$purchase_price[$k];
				$sku_list['id']=$v;
				//$data['purchase_price'] = $sku_list['purchase_price'];

				//查询该SKU属于哪种类型的产品，为价格公式查找做准备
				//$type = M('product')->field('product_type')->where(array('sku'=>$sku_list['sku']))->find();
				//查询这个商品在产品目录的指导价
				$product_price = M('product')->field('ali_lt,ali_gt,cost_price')->where(array('sku'=>$sku_list['sku']))->find();
				
				//判断目录里的指导价是否与子订单的指导价一致
				if($info_old['quantity']<50){
					//如果子订单内的采购数量小于50，且产品目录内的指导价与子订单内的销售价一致 根据采购数量自动修改价格
					if($product_price['ali_lt']==$info_old['purchase_price'] && $purchase_price[$k]==$product_price['ali_lt']){
						//如果修改的数量小于50
						if($sku_list['quantity']<50){
							$data['purchase_price'] = $product_price['ali_lt'];
						}else{
							$data['purchase_price'] = $product_price['ali_gt'];
						}
						
					}else{
						$data['purchase_price'] = $purchase_price[$k];
					}
				}else{
					//如果子订单内的采购数量大于或等于50，且产品目录内的指导价与子订单内的销售价一致并且没有手动修改价格  根据采购数量自动修改价格
					if($product_price['ali_gt']==$info_old['purchase_price'] && $purchase_price[$k]==$product_price['ali_gt']){

						//如果修改的数量小于50 
						if($sku_list['quantity']<50){
							$data['purchase_price'] = $product_price['ali_lt'];
						}else{
							$data['purchase_price'] = $product_price['ali_gt'];
						}
					}else{
						$data['purchase_price'] = $purchase_price[$k];
					}
				}
				$data['total_price']=$data['purchase_price']*$sku_list['quantity'];
				$data['total_cost']=$sku_list['cost_price']*$sku_list['quantity'];
				
				$res = M('order_info')->where(array('id'=>$sku_list['id']))->save(array('quantity'=>$sku_list['quantity'],'purchase_price'=>$data['purchase_price'],'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost']));
				
			}
			$this->success('修改成功！');exit();
			
		}
		
		$acc_type = M('account')->field('look_details')->where(array('username'=>$_SESSION['user']))->find();
		$this->assign('acc_type',$acc_type);	
		$this->assign("order_info",$order_info);	
		$this->assign("pay_list",$payment_list);
		$this->assign("info",$info_list);	
		$this->assign("count",$count);				  
		$this->display();
	}
	
	//订单删除
	public function order_dele(){
		$order_id=I("get.id","","htmlspecialchars");
		if($order_id){
			$order_number = M('order')->field('order_number')->where(array('id'=>$order_id))->find();
			$res = M("order")->where(array('id'=>$order_id))->delete();
			$result = M('order_info')->where(array('order_number'=>$order_number['order_number']))->delete();
			$this->success("删除成功！");
		}
		
	}
	
	//可发货订单明细
	public function delivery_details(){
		$where['id']=$_REQUEST['id'];
		$order = M("order");
		$number = $order->field("order_number")->where($where)->find();
		$order_number = $number["order_number"];
		$order_details = M('order_info')->where(array('order_number'=>$order_number,'sales_return'=>'','status'=>'3'))->select();
		$acc_type = M('account')->field('look_details')->where(array('username'=>$_SESSION['user']))->find();
		$this->assign('acc_type',$acc_type);
		$this->assign("order_details",$order_details);
		$this->display();
	}
	
	
	//待采购订单删除
	public function wait_dele(){
		$name = $_SESSION['user'];
		$account = M("account")->field("to_purchase")->where(array('username'=>$name))->find();
		
		$to_purchase = $account['to_purchase'];
		if($to_purchase=='2'){
			$this->error("删除失败,您没有权限");
		}else{
			$where['id']=$_REQUEST['id'];
			$k=$_REQUEST['k'];
			$order = M("order");
			$number = $order->field("order_number")->where($where)->find();
			$order_number = $number['order_number'];
			$res = M('order_info')->where(array("order_number"=>$order_number,/*'status'=>'3',*/'sales_return'=>''))->select();
			foreach($res as $v){
				if($v['purchase_quantity']=='0' && $v['cumulative_quantity']=='0'){
					$res1 = M('order_info')->where(array("id"=>$v['id']))->delete();	
				}else{
					//var_dump($v);die;
					$quantity = $v['purchase_quantity']+$v['cumulative_quantity'];
					$where = " order_number = '".$v['order_number']."' and sku='".$v['sku']."' and quantity>0";
					$check = M('sales_return')->where($where)->find();
					if($check){
						$arr = M('sales_return')->where(array('order_number'=>$v['order_number'],'sku'=>$v['sku']))->save(array('quantity'=>$quantity));
					}else{
						$arr = M('sales_return')->add(array('buyer'=>$v['buyer'],'supplier'=>$v['supplier'],'salesman'=>$v['salesman'],'sku'=>$v['sku'],'quantity'=>$quantity,'order_number'=>$v['order_number'],'info_id'=>$v['id']));
					}
					$res1 = M('order_info')->where(array("id"=>$v['id']))->delete();	
				}
			
			}
			
			$res2 = M('order')->where(array("id"=>$where['id']))->delete();
			
		}
			if($res2){
				$this->success("删除成功！");
			}
			
	}
	
	//待采购订单删除
	//public function wait_dele(){
//		$name = $_SESSION['user'];
//		$account = M("account")->field("to_purchase")->where(array('username'=>$name))->find();
//		
//		$to_purchase = $account['to_purchase'];
//		if($to_purchase=='2'){
//			$this->error("删除失败,您没有权限");
//		}else{
//			$where['id']=$_REQUEST['id'];
//			$k=$_REQUEST['k'];
//			$order = M("order");
//			$number = $order->field("order_number")->where($where)->find();
//			$order_number = $number['order_number'];
//			$res = M('order_info')->where(array("order_number"=>$order_number,/*'status'=>'3',*/'sales_return'=>''))->select();
//			foreach($res as $v){
//				if($v['is_create']=='1'){
//					if($v['purchase_quantity']=='0' && $v['cumulative_quantity']=='0'){
//						$res = M('order_info')->where(array("id"=>$v['id']))->delete();	
//					}else{
//						$res = M('order_info')->where(array("id"=>$v['id']))->save(array('sales_return'=>'1'));		
//						$result = M('order')->where($where)->save(array('sales_return'=>'1'));
//					}
//				}else{
//					$res = M('order')->where(array("order_number"=>$v['order_number']))->delete();	
//				}
//			}
//			$result = M('order_info')->where(array("order_number"=>$order_number,'status'=>'2'))->select();
//			
//			if($result ==NULL){
//				$res = M('order')->where(array("id"=>$where['id']))->delete();
//			}	
//		}
//			if($res){
//				$this->success("删除成功！");
//			}
//			
//	}
	
	//可发货订单删除
	public function delivery_dele(){
		$name = $_SESSION['user'];
		$account = M("account")->field("delivery")->where(array('username'=>$name))->find();
		
		$delivery = $account['delivery'];
		if($delivery=='2'){
			$this->error("删除失败,您没有权限");
		}else{
			$where['id']=$_REQUEST['id'];
			$order = M("order");
			$order_info = M("order_info");
			$number = $order->field("order_number")->where($where)->find();
			$order_number = $number['order_number'];
			$order_info = $order_info->where(array('order_number'=>$order_number))->select();
			foreach($order_info as $k=>$v){
				$check = M('sales_return')->where(array('order_number'=>$order_number,'sku'=>$v['sku']))->find();
				if($check==NULL){
					$res = M('sales_return')->add(array('buyer'=>$v['buyer'],'supplier'=>$v['supplier'],'salesman'=>$v['salesman'],'sku'=>$v['sku'],'quantity'=>$v['quantity'],'info_id'=>$v['id']));
					$res1 = M('order_info')->where(array("order_number"=>$order_number,'sku'=>$v['sku']))->delete();				
				}else{
					$quantity = $check['quantity']+$v['quantity'];
					$res = M('sales_return')->save(array('quantity'=>$quantity));
					$res1 =  M('order_info')->where(array("order_number"=>$order_number,'sku'=>$v['sku']))->delete();
				}
			}
			$result = $order->where($where)->delete();
			
			//$res = $order_info->where(array("order_number"=>$order_number))->delete();
			
			header("Location:".$_SERVER['HTTP_REFERER']);
		}
		
	}
	
	
	//待采购列表
	public function wait_purchase(){
		
		$wheres['status']='2';
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$order_number	=	I('get.order_number','','htmlspecialchars');
		$salesman		=	I('get.salesman','','htmlspecialchars');
		$customer_name	=	I('get.customer_name','','htmlspecialchars');
		/*$wheres = '1';
		if($order_number!=''){
			$wheres['order_number'] = $order_number;
		}
		if($salesman!=''){
			$wheres['salesman'] = $salesman;
		}
		if($customer_name!=''){
			$wheres	['customer_name'] = $customer_name;
		}*/
		$wheres['order_number'] = $order_number;
		$wheres['salesman'] = $salesman;
		$wheres	['customer_name'] = $customer_name;
		
		
		
		$name = $_SESSION['user'];
		$account = M("account")->field("orders")->where(array('username'=>$name))->find();
		$orders = $account['orders'];
		$wheres['type'] = $account['orders'];
		$order = D("orders");
		$res = $order->wait_list($pageNo,$pageSize,$wheres);
		
		$count = $res['count'];
		$order_list = $res['order_list'];
		foreach($order_list as $k=>$v){
			$order_number = $v['order_number'];
			$order_info = M("order_info")->where(array('order_number'=>$order_number,'sales_return'=>''))->sum("total_price");
			
			$order_list[$k]['total_price'] = $order_info;
			
			
			
			$str =$order_list[$k]['total_price']*$v['discount_rate'];
			$arr = explode('.',$str);
			$num=substr($arr[1],0,2);
			$discount_rate = $arr[0].'.'.$num;
			
			
			$order_list[$k]['discounted_value'] = $discount_rate;
			
		}
		$this->assign("order_number",$search_number);
		$this->assign('salesman',$salesman);
		$this->assign("customer_name",$customer_name);
		$this->assign("order_list",$order_list);
		$this->assign('orders',$orders);
		$this->assign("count",$count);
		$this->display();
	}
	
	//待采购订单明细
	public function wait_details(){
		$where['id']=I('get.id','','htmlspecialchars');
		$order = M("order");
		$number = $order->field("order_number")->where($where)->find();
		$order_number = $number["order_number"];
		$order_details = M('order_info')->where(array('order_number'=>$order_number,'sales_return'=>''))->select();
		$this->assign("order_details",$order_details);
		$this->display();
	}
	
	//采购单编辑
	public function edit_purchase(){
		$id = I("id","","htmlspecialchars");
		$result = M("order")->where(array('id'=>$id))->find();
		$info = M('order')->where(array('id'=>$id))->find();
		$pay_list = M('payment')->select();
		
		$order_number = $result['order_number'];
		
		$order_info = M("order_info")->where(array('order_number'=>$order_number,'sales_return'=>''))->select();
		
		
		$buyer_list = M('order_info')->field('buyer')->where(array('order_number'=>$order_number,'sales_return'=>''))->group('buyer')->select();
		
			
		$buyer = M('order_info')->field('buyer')->where(array('order_number'=>$order_number,'sales_return'=>''))->group('buyer')->select();
		$index = 0;
		foreach($buyer as $k=>$v){
			$arr = M('order_info')->field('buyer')->where(array('order_number'=>$order_number,'buyer'=>$v['buyer'],'sales_return'=>''))->select();
			$buyer[$k]['count'] = count($arr);
			
			
			$buyer[$k]['index'] = $index;
			$index = $index +count($arr);
			
		}
		foreach($buyer as $key=>$val){
			foreach($order_info as $k=>$v){
				if($k==$val['index']){
					$order_info[$k]['count'] = $val['count'];
				}
			}
			
		}
		//待采购订单编辑
		if(I('post.submit','','htmlspecialchars')){
			$data['salesman'] = I('post.salesman','','htmlspecialchars');
			$data['customer_name'] = I('post.customer_name','','htmlspecialchars');
			$data['order_number'] = I('post.order_number','','htmlspecialchars');
			$data['consignee'] = I('post.consignee','','htmlspecialchars');
			$data['email'] = I('post.email','','htmlspecialchars');
			$data['phone'] = I('post.phone','','htmlspecialchars');
			$data['feright'] = I('post.feright','','htmlspecialchars');
			$discount_rate = I('post.discount_rate','','htmlspecialchars');
			$data['discount_rate'] 	= empty($discount_rate)?'1':$discount_rate;
			$data['pay_method'] = I('post.payment_method','','htmlspecialchars');
			$data['address'] = I('post.address','','htmlspecialchars');
			$data['orders_information'] = I('post.orders_information','','htmlspecialchars');
			$data['salesman'] = I('post.salesman','','htmlspecialchars');
			$order_list=explode("\n",trim($data['orders_information'],"\n"));
			$edit_info = M('order')->where(array('order_number'=>$data['order_number']))->save($data);
			$rate = M('order_info')->where(array('order_number'=>$data['order_number']))->save(array('discount_rate'=>$data['discount_rate']));
			foreach($order_list as $k=>$v){
				$res = explode("	",trim($v,"	"));
				$sku =$res['0'];
				$product = M("product");
				$where['sku'] = $sku;
				$where['status'] = '1';
				$product_info = $product->where($where)->find();
				$p_id = $product_info['id'];
				$supplier = $product_info['supplier'];
				$buyer = $product_info['buyer'];
				$main_pic = $product_info['size_pic'];
				$cost_price = $product_info['cost_price'];	
				if(!empty($product_info)){
					$result = explode("	",trim($v," "));
					$sku = $result['0'];
					$quantity = trim($result['1']);
					$order_info = M('order_info')->where(array('sku'=>$sku,'order_number'=>$data['order_number'],'sales_return'=>''))->find();
					$data['id'] = $order_info['id'];
					if($order_info){
						$order_info = D("orders");
						
						$result_info = $order_info->wait_info($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price);
					}else{
						$order_info = D("orders");
						$result_info = $order_info->edit_wait($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price);
					}
				}else{
					$title[] = $sku;
					$msg[$k] =  $sku."<br/>";
					
					continue;
				}
				 
			}
			if(empty($msg)){
				$this->success("添加成功","",'3');exit();
			}else{
				$order_list=explode("\n",trim($orders_information,"\n"));
				if($order_list['0']=='' && empty($msg)){
					$this->success("修改成功","",'3');exit();
				}else{
					echo "<div style='font-size:26px;'>以下产品不存在 ,请及时复制出以下产品,检查后编辑对应订单!</div><br/>";
					foreach($msg as $k=>$v){
						echo $v;
					}
					exit();
				}
				
			}

			
		}
		
		if(I("dele","","htmlspecialchars")){
			
			$id = I("dele","","htmlspecialchars");
			$quantity = M('order_info')->field('buyer,supplier,salesman,sku,quantity,order_number,purchase_quantity,is_create,id,cumulative_quantity')->where(array('id'=>$id))->find();
			
			if($quantity['is_create']=='1' || $quantity['cumulative_quantity']>0){	//如果已经生成采购单
				if($quantity['cumulative_quantity']>0){												//如果已经有采购数量
					$result = M('order_info')->where(array('id'=>$id))->save(array('sales_return'=>1));	//将订单订单变为退货状态
					$res = M('sales_return')->add(array('buyer'=>$quantity['buyer'],'supplier'=>$quantity['supplier'],'salesman'=>$quantity['salesman'],'sku'=>$quantity['sku'],'quantity'=>$quantity['cumulative_quantity'],'order_number'=>$quantity['order_number'],'info_id'=>$quantity['id']));
					
				}else{
					$result = M('order_info')->where(array('id'=>$id))->save(array('sales_return'=>1));	//将订单订单变为退货状态
					$res = M('sales_return')->add(array('buyer'=>$quantity['buyer'],'supplier'=>$quantity['supplier'],'salesman'=>$quantity['salesman'],'sku'=>$quantity['sku'],'quantity'=>$quantity['cumulative_quantity'],'order_number'=>$quantity['order_number'],'info_id'=>$quantity['id']));
				}
				
				
			}else{
				$res = M('order_info')->where(array('id'=>$id))->delete();
			}
			$this->success("删除成功！");exit;
			
		}
		
		//直接修改数量
		if(I("edit","","htmlspecialchars")){
			$id = I("ids","","htmlspecialchars");
			$sku = I("sku","","htmlspecialchars");
			$quantity = I("quantity","","htmlspecialchars");
			$purchase_price	=	I("purchase_price","","htmlspecialchars");
			$order = array();
			foreach($id as $k=>$v){
				$id = $v;
				$sku_list = M('order_info')->field('id,sku,cost_price,quantity,purchase_price')->where(array('id'=>$id))->find();
				
				$info_old = $sku_list;
				$sku_list['quantity']=$quantity[$k];
				$sku_list['id']=$v;
				$type = M('product')->field('product_type')->where(array('sku'=>$sku_list['sku']))->find();
				//查询这个商品在产品目录的指导价
				$product_price = M('product')->field('ali_lt,ali_gt,cost_price')->where(array('sku'=>$sku_list['sku']))->find();
				
				//判断目录里的指导价是否与子订单的指导价一致
				if($info_old['quantity']<50){
					//如果子订单内的采购数量小于50，且产品目录内的指导价与子订单内的销售价一致 根据采购数量自动修改价格
					if($product_price['ali_lt']==$info_old['purchase_price'] && $purchase_price[$k]==$product_price['ali_lt']){
						//如果修改的数量小于50
						if($sku_list['quantity']<50){
							$data['purchase_price'] = $product_price['ali_lt'];
						}else{
							$data['purchase_price'] = $product_price['ali_gt'];
						}
						
					}else{
						$data['purchase_price'] = $purchase_price[$k];
					}
				}else{
					//如果子订单内的采购数量大于或等于50，且产品目录内的指导价与子订单内的销售价一致  根据采购数量自动修改价格
					if($product_price['ali_gt']==$info_old['purchase_price'] && $purchase_price[$k]==$product_price['ali_gt']){
						//如果修改的数量小于50
						if($sku_list['quantity']<50){
							$data['purchase_price'] = $product_price['ali_lt'];
						}else{
							$data['purchase_price'] = $product_price['ali_gt'];
						}
					}else{
						$data['purchase_price'] = $purchase_price[$k];
					}
				}
				//$data['purchase_price']	=	$purchase_price[$k];
				$data['total_price'] = $data['purchase_price']*$quantity[$k];
				$data['total_cost']=$sku_list['cost_price']*$quantity[$k];
				$result = M('order_info')->where(array('id'=>$v))->find();
				$alsobuy = $quantity[$k]- $result['cumulative_quantity'];
				
				if($result['cumulative_quantity']>=$quantity[$k]){
					$cumulative_quantity = $result['quantity'];
					$sales_quantity = $result['cumulative_quantity']+$result['purchase_quantity']-$quantity[$k];
					$check = M('sales_return')->where(array('order_number'=>$result['order_number'],'sku'=>$result['sku']))->find();
				
					if($check){
						$add_sales = M('sales_return')->where(array('id'=>$check['id']))->save(array('supplier'=>$result['supplier'],'buyer'=>$result['buyer'],'salesman'=>$result['salesman'],'sku'=>$result['sku'],'order_number'=>$result['order_number'],'info_id'=>$result['id'],'quantity'=>$sales_quantity));
					}else{
						$add_sales = M('sales_return')->add(array('supplier'=>$result['supplier'],'buyer'=>$result['buyer'],'salesman'=>$result['salesman'],'sku'=>$result['sku'],'order_number'=>$result['order_number'],'info_id'=>$result['id'],'quantity'=>$sales_quantity));
					}
					$res = M('order_info')->where(array('id'=>$sku_list['id']))->save(array('quantity'=>$sku_list['quantity'],'purchase_price'=>$data['purchase_price'],'total_price'=>$data['total_price'],'alsobuy'=>$alsobuy,'total_cost'=>$data['total_cost']));
				}else{
					$check = M('sales_return')->where(array('order_number'=>$result['order_number'],'sku'=>$result['sku']))->find();
			
					if($result['alsobuy']>$check['quantity']){
						$dele = M('sales_return')->where(array('id'=>$check['id']))->delete();
					}else{
						$sales_quantity = $result['alsobuy']-$check['quantity'];
						$add_sales = M('sales_return')->where(array('id'=>$check['id']))->save(array('supplier'=>$result['supplier'],'buyer'=>$result['buyer'],'salesman'=>$result['salesman'],'sku'=>$result['sku'],'order_number'=>$result['order_number'],'info_id'=>$result['id'],'quantity'=>$sales_quantity));
					}
					
					$res = M('order_info')->where(array('id'=>$sku_list['id']))->save(array('quantity'=>$sku_list['quantity'],'purchase_price'=>$data['purchase_price'],'total_price'=>$data['total_price'],'alsobuy'=>$alsobuy,'total_cost'=>$data['total_cost']));
				
				}
				
			}
			$this->success('修改成功！');exit();
			
		}
		
		$count = count($order_info);
		//var_dump($buyer_list);
		$acc_type = M('account')->field('look_details')->where(array('username'=>$_SESSION['user']))->find();
		$this->assign('acc_type',$acc_type);
		$this->assign('info',$info);
		$this->assign('buyer_list',$buyer_list);
		$this->assign('count',$count);
		$this->assign('pay_list',$pay_list);
		$this->assign("order_info",$order_info);
		$this->display();
	}

	//待采购订单变为可发货订单
	public function order_delivery(){
		$order_id=I('get.id','','htmlspecialchars');
		$order_number = M('order')->field('order_number')->where(array('id'=>$order_id))->find();
		$order_info = M('order_info')->field('quantity,purchase_quantity,cumulative_quantity,id')->where(array('order_number'=>$order_number['order_number'],'sales_return'=>''))->select();
		
		foreach($order_info as $val){
			if($val['quantity'] > $val['cumulative_quantity']){
				$this->error("无法发货，订单还有未采购完成的商品！");
				$msg = '1';
			}
		}
		if(empty($msg)){
			$result = M('order')->where(array('id'=>$order_id))->save(array('status'=>'3'));
			$results = M('order_info')->where(array('ordder_number'=>$order_number['order_number']))->save(array('status'=>'3'));
			$res = M('order_info')->where(array('order_number'=>$order_number['order_number'],'sales_return'=>'1'))->find();
			$sales = M('sales_return')->add(array('buyer'=>$res['buyer'],'supplier'=>$res['supplier'],'salesman'=>$res['salesman'],'sku'=>$res['sku'],'quantity'=>$res['purchase_quantity'],'order_number'=>$res['order_number'],'info_id'=>$res['id']));
			$this->success('提交成功');exit();
		}
		
	}
	
	
	//转为可发货列表
	public function delivery_list(){
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$name = $_SESSION['user'];
		$account = M("account")->field("orders")->where(array('username'=>$name))->find();
		$type = $account['orders'];
		
		$wheres['type']	=	$account['orders'];
		$wheres['order_number'] = I('get.order_number','','htmlspecialchars');
		$wheres['customer_name'] = I('get.customer_name','','htmlspecialchars');
		//$wheres['salesman'] = I('get.salesman','','htmlspecialchars');
		$wheres['salesman'] = I('get.salesman','','htmlspecialchars');
		
		
		$order = D("orders");
		$res = $order->delivery_list($pageNo,$pageSize,$wheres);
		$count = $res['count'];
		$order_list = $res['order_list'];
		/*if($orders=='2'){
			$order = D("orders");
			$own = $name;
			$res = $order->delivery_list_own($pageNo,$pageSize,$wheres,$own);
			$count = $res['count'];
			$order_list = $res['order_list'];
		}else{
			$order = D("orders");
			$res = $order->delivery_list($pageNo,$pageSize,$wheres);
			$count = $res['count'];
			$order_list = $res['order_list'];
		}
		*/
		
		$order = D("orders");
		$res = $order->delivery_list($pageNo,$pageSize,$wheres);
		$count = $res['count'];
		$order_list = $res['order_list'];
		
		foreach($order_list as $k=>$v){
			$order_number = $v['order_number'];
			$order_info = M("order_info")->where(array('order_number'=>$order_number,'sales_return'=>''))->sum("total_price");
			$order_list[$k]['total_price'] = $order_info;
			
			
			$str =$order_list[$k]['total_price']*$v['discount_rate'];
			$arr = explode('.',$str);
			$num=substr($arr[1],0,2);
			$discount_rate = $arr[0].'.'.$num;
			
			$order_list[$k]['discounted_value'] = $discount_rate;
			
		}
		
		if(I("value","","htmlspecialchars")){
			$remarks = I("value","","htmlspecialchars");
			$id = I("order_id","","htmlspecialchars");
			$order = D("orders");
			$add_remarks = $order->add_remarks($id,$remarks);
			
		}
		
		$this->assign('name',$name);
		$this->assign("order_number",$wheres['order_number']);
		$this->assign("customer_name",$wheres['customer_name']);
		$this->assign("salesman",$wheres['salesman']);
		$this->assign('type',$type);
		
		
		$this->assign("order_list",$order_list);
		$this->assign("count",$count);
		$this->display();
	}
	
	
	
	//可发货订单变为已发货订单
	public function order_perform(){
		$order_id=I('get.id','','htmlspecialchars');
		$data['delivery_time'] = date("Y-m-d H:i:s",time());
		$data['status'] = '4';
		$order = M("order");
		$result = $order->where(array('id'=>$order_id))->save($data);
		$order_number=I('get.order_number','','htmlspecialchars');
		$res = M('order_info')->where(array('order_number'=>$order_number,'sales_return'=>''))->save(array('status'=>'4'));
		if($res && $result){
			header("Location:".$_SERVER['HTTP_REFERER']);
		}
		
	}
	
	
	//已发货订单列表
	public function complete_list(){
		$wheres['status']='4';
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		
		$name = $_SESSION['user'];
		$account = M("account")->field("orders")->where(array('username'=>$name))->find();
		$orders = $account['orders'];
		/*if($orders=='2'){
			$order = D("orders");
			$own = $name;
			$res = $order->complete_list_own($pageNo,$pageSize,$wheres,$own);
		}else{
			$order = D("orders");
			$res = $order->complete_list($pageNo,$pageSize,$wheres);
		}*/
		
		$wheres['type']	=	$account['orders'];
		
		$wheres['order_number'] = I('get.order_number','','htmlspecialchars');
		$wheres['customer_name'] = I('get.customer_name','','htmlspecialchars');
		
		$wheres['salesman'] = I('get.salesman','','htmlspecialchars');

		
		$order = D("orders");
		$res = $order->complete_list($pageNo,$pageSize,$wheres);
			
			
		
		$count = $res['count'];
		$complete_list = $res['complete_list'];
			
		foreach($complete_list as $k=>$v){
			$order_number = $v['order_number'];
			$order_info = M("order_info")->where(array('order_number'=>$order_number,'sales_return'=>''))->sum("total_price");
			
			$complete_list[$k]['total_price'] = $order_info;
			
			
			$str = $complete_list[$k]['total_price']*$v['discount_rate'];
			$arr = explode('.',$str);
			$num=substr($arr[1],0,2);
			$discount_rate = $arr[0].'.'.$num;
			
			$complete_list[$k]['discounted_value'] = $discount_rate;
			
		}
		
		$this->assign("order_number",$wheres['order_number']);
		$this->assign("customer_name",$wheres['customer_name']);
		$this->assign("salesman",$wheres['salesman']);
		
		$this->assign('orders',$orders);
		$this->assign("count",$count);
		$this->assign("complete_list",$complete_list);
		$this->display();
	}
	
	//草稿箱订单明细
	public function complete_details(){
		$where['id']=$_REQUEST['id'];
		$order = M("order");
		$number = $order->field("order_number")->where($where)->find();
		$order_number = $number["order_number"];
		$order_model = D("orders");
		$order_details = $order_model->details_info($order_number);

		$acc_type = M('account')->field('look_details')->where(array('username'=>$_SESSION['user']))->find();
		$this->assign('acc_type',$acc_type);
		$this->assign("order_details",$order_details);
		$this->display();
	}
	
	//已发货订单删除
	public function complete_dele(){
		$order_id = I('get.id','','htmlspecialchars');
		$name = $_SESSION['user'];
		$account = M("account")->field("complete")->where(array('username'=>$name))->find();
		if($account['complete']!='1'){
			$this->error("删除失败,您没有权限");
		}else{
			//$order_number = M('order')->field('order_number')->where(array('id'=>$order_id))->find();
			//$result = M('order')->where(array('id'=>$order_id))->delete();
			
			
			$order = M("order");
			$order_info = M("order_info");
			$number = $order->field("order_number")->where(array('id'=>$order_id))->find();
			$order_number = $number['order_number'];
			$order_info = $order_info->where(array('order_number'=>$order_number))->select();
			foreach($order_info as $k=>$v){
				$check = M('sales_return')->where(array('order_number'=>$order_number,'sku'=>$v['sku']))->find();
				
				if($check==NULL){
					$res = M('sales_return')->add(array('buyer'=>$v['buyer'],'supplier'=>$v['supplier'],'salesman'=>$v['salesman'],'sku'=>$v['sku'],'quantity'=>$v['quantity'],'info_id'=>$v['id'],'order_number'=>$order_number));
					$res1 =  M('order_info')->where(array("order_number"=>$order_number,'sku'=>$v['sku']))->delete();				
				}else{
					$quantity = $check['quantity']+$v['quantity'];
					$res = M('sales_return')->save(array('quantity'=>$quantity));
					$res1 =  M('order_info')->where(array("order_number"=>$order_number,'sku'=>$v['sku']))->delete();
				}
			}
			$result = M('order')->where(array('id'=>$order_id))->delete();
			if( $result){
				$this->success("删除成功！");
			}
		}
		
	}
	
	//已完成订单明细
	public function stock_details(){
		$where['id']=$_REQUEST['id'];
		$order = M("order");
		$sku = $order->field("sku,quantity")->where($where)->find();
		$arr = explode("*",trim($sku['sku'],'*'));
		$arr2 = explode("*",trim($sku['quantity'],'*'));
		foreach($arr as $key=>$val){
			$orders = D("orders");
			$wheres = $val;
			$res[] = $orders->details_list($wheres);
		}
		
		$this->assign("res_list",$res);
		$this->assign("quantity",$arr2);
		$this->display();
	}
	
	
	/**
		* 导出数据为excel表格
		*@param $data    一个二维数组,结构如同从数据库查出来的数组
		*@param $title   excel的第一行标题,一个数组,如果为空则没有标题
		*@param $filename 下载的文件名
		*@examlpe 
		$stu = M ('User');
		$arr = $stu -> select();
		exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
	*/
	public function exportexcel($data=array(),$title=array(),$filename='report'){
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:applicationnd.ms-excel");  
		header("Content-Disposition:attachment;filename=".$filename.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		//导出xls 开始
		if (!empty($title)){
			foreach ($title as $k => $v) {
				$title[$k]=iconv("UTF-8", "GB2312",$v);
				//$title[$k]=$v;
			}
			$title= implode("\t", $title);
			echo "$title\n";
		}
		if (!empty($data)){
			foreach($data as $key=>$val){
				foreach ($val as $ck => $cv) {
					$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
				}
				$data[$key]=implode("\t", $data[$key]);
				
			}
			echo implode("\n",$data);
		}
		
	}

	

}


  

?>